package org.login.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.login.logic.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

@RestController
@CrossOrigin
public class LoginController {

	@Reference(version = "1.0.0", application = "${dubbo.application.id}", url = "dubbo://localhost:12345")
	private LoginService loginService;

	private HttpServletRequest req;
	
	@Autowired
	private StringRedisTemplate redis;
	
	public LoginController(HttpServletRequest req) {
		// TODO Auto-generated constructor stub
		this.req = req;

	}

	@RequestMapping("/sayHello")
	public Map<String, Object> loginAdmin() {
		
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();

		Enumeration<String> param = this.req.getParameterNames();
		while (param.hasMoreElements()) {
			String paramName = param.nextElement();

			String paramValue = this.req.getParameter(paramName);
			// 形成键值对应的map
			map.put(paramName, paramValue);
		}
		
		Map<String, Object> obj = this.loginService.login(map);
		
		ValueOperations<String, String> result = this.redis.opsForValue();
		
		result.set("admin_id", String.valueOf(obj.get("id")));
		result.set("admin_name", String.valueOf(obj.get("admin_name")));
		obj.remove("id");
		return obj;
	}
	
	@RequestMapping("/getId")
	public String getId()
	{
		return this.redis.opsForValue().get("admin_id");
	}
}
